package com.oracle.third.service.mobile.listener.service;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.oracle.third.utils.CodeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class MobileService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${cache_key_prefix}")
    private String cachePrefix;

    //短信相关
    @Value("${product}")
    private String product;
    @Value("${domain}")
    private String domain;
    @Value("${accessKeyId}")
    private String accessKeyId;
    @Value("${accessKeySecret}")
    private String accessKeySecret;
    @Value("${signName}")
    private String signName;
    @Value("${templateCode}")
    private String templateCode;


    /**
     * 发送手机短信 并把验证码保存到redis中
     * @param phone
     */
    public void sendMobileRandom(String phone){
        //redis的key
        String cacheKey = this.cachePrefix+"mobile:"+phone;
        String reSendKey = this.cachePrefix+"reSendPhone:"+phone;
        //判断是否达到的可以重新发送验证码的时间
        Object obj = this.redisTemplate.opsForValue().get(reSendKey);
        if (obj!=null){
            System.out.println("------------>未达到可重新发送的时间");
            return;
        }
        //生成验证码
        String randomCode = CodeUtils.createCode();
        //发送短信
        this.sendSms(phone,randomCode);
        //把验证码存储redis
        this.redisTemplate.opsForValue().set(cacheKey,randomCode,3,TimeUnit.MINUTES);
        //设置可重新发送短信的时间
        this.redisTemplate.opsForValue().set(reSendKey,"re send phone",1,TimeUnit.MINUTES);
    }


    /**
     * 发送短信
     * @param phone
     * @param randomCode
     */
    public void sendSms(String phone,String randomCode){
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName("Poseidon管理中心");
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode("SMS_151770066");
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        request.setTemplateParam("{\"code\":"+randomCode+"}");

        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        try {
            acsClient.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }


}
